package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.dto.SetDefaultVo;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 小帅蓝
 * @Date 2022/6/22 19:10
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Resource
    private AddressBookMapper addressBookMapper;

    @Override
    public R<AddressBook> queryDefault() {
        AddressBook addressBook = addressBookMapper.queryDefault(TokenHolder.getCurrentId());
        if (addressBook != null) {
            return R.success(addressBook);
        }
        return R.error("获取默认收货地址失败！");
    }

    @Override
    public R<AddressBook> setDefault(AddressBook addressBook) {
        String userId = TokenHolder.getCurrentId();
        addressBook.setIsDefault(1);
        //判断用户有没有默认地址，有删除之前的默认地址，没有直接设置
        AddressBook book = addressBookMapper.queryDefault(userId);
        if (book != null) {
            int row = addressBookMapper.updateDefault(userId);
            if (row > 0) {
                addressBook.setUpdateUser(TokenHolder.getCurrentId());
                int row2 = addressBookMapper.updateAddressBook(addressBook);
                AddressBook address = addressBookMapper.queryById(addressBook.getId());
                return row2 > 0 ? R.success(address) : R.error("设置默认失败！");
            }
            return R.error("设置默认地址失败！");
        }
        addressBook.setUpdateUser(TokenHolder.getCurrentId());
        int row3 = addressBookMapper.updateAddressBook(addressBook);
        AddressBook address = addressBookMapper.queryById(addressBook.getId());
        return row3 > 0 ? R.success(address) : R.error("设置默认失败！");
    }

    @Override
    public R<List<AddressBook>> queryAll() {
        return R.success(addressBookMapper.queryAll(TokenHolder.getCurrentId()));
    }

    @Override
    public R<AddressBook> queryById(String id) {
        AddressBook addressBook = addressBookMapper.queryById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        }
        return R.error("根据id获取收货地址失败！");
    }

    @Override
    public R<AddressBook> addAddressBook(AddressBook addressBook) {
        addressBook.setId(new Snowflake().nextIdStr());
        addressBook.setUserId(TokenHolder.getCurrentId());
        addressBook.setCreateUser(TokenHolder.getCurrentId());
        addressBook.setUpdateUser(TokenHolder.getCurrentId());
        int row = addressBookMapper.addAddressBook(addressBook);
        if (row > 0) {
            return R.success(addressBookMapper.queryById(addressBook.getId()));
        }
        return R.error("添加收货地址失败！");
    }

    @Override
    public boolean deleteAddressBook(String ids) {
        String[] idsArr = ids.split(",");
        List<String> idList = Arrays.stream(idsArr).collect(Collectors.toList());
        int row = addressBookMapper.deleteAddressBook(idList);
        return row > 0;
    }

    @Override
    public R<String> updateAddressBook(AddressBook addressBook) {
        addressBook.setUserId(TokenHolder.getCurrentId());
        int row = addressBookMapper.updateAddressBook(addressBook);
        return row > 0 ? R.success("修改成功！") : R.error("修改失败！");
    }

}
